export interface ILevelConfig {
    id: number;
    colorKind: number;
    carNums: number[];
    layers: ILayerInfo[];
}

export interface ILayerInfo {
    id: number;
    path: string;
    pos?: cc.Vec2;
    isGray: boolean;
    layer?: cc.Node;
    boards: IBoardInfo[];
}

export interface IBoardInfo {
    id: number;
    color: number;
    node: cc.Node;
    screwInfos: IScrewInfo[];
}

export interface IScrewInfo {
    id: number;
    color: number;
    node: cc.Node;
    revoluteJoint: cc.RevoluteJoint;
}

export default class LevelConfig {
    private static _instance: LevelConfig;

    public static get instance(): LevelConfig {
        if (!LevelConfig._instance) {
            LevelConfig._instance = new LevelConfig();
        }
        return LevelConfig._instance;
    }

    // private configs: ILevelConfig[] = [
    //     {
    //         id: 1,
    //         colorKind: 2,
    //         carNums: [0, 0, 0],
    //         layers:
    //             [
    //                 // { id: 1, path: 'tu/biji1', pos: cc.v2(0, 0), boards: [], isGray: false },
    //                 // { id: 1, path: 'tu/biji2', pos: cc.v2(0, 0), boards: [], isGray: false },
    //                 // { id: 1, path: 'tu/chuizi1', pos: cc.v2(0, 0), boards: [], isGray: false },
    //                 // { id: 1, path: 'tu/chuizi2', pos: cc.v2(0, 0), boards: [], isGray: false },
    //                 // { id: 1, path: 'tu/dianhua1', pos: cc.v2(0, 0), boards: [], isGray: false },
    //                 // { id: 1, path: 'tu/dianhua2', pos: cc.v2(0, 0), boards: [], isGray: false },
    //                 // { id: 1, path: 'tu/gaodian1', pos: cc.v2(0, 0), boards: [], isGray: false },
    //                 // { id: 1, path: 'tu/gaodian2', pos: cc.v2(0, 0), boards: [], isGray: false },

    //                 // { id: 1, path: 'tu/guo1', pos: cc.v2(0, 0), boards: [], isGray: false },
    //                 // { id: 1, path: 'tu/guo2', pos: cc.v2(0, 0), boards: [], isGray: false },
    //                 // { id: 1, path: 'tu/juchang1', pos: cc.v2(0, 0), boards: [], isGray: false },
    //                 // { id: 1, path: 'tu/juchang2', pos: cc.v2(0, 0), boards: [], isGray: false },
    //                 // { id: 1, path: 'tu/kouhong1', pos: cc.v2(0, 0), boards: [], isGray: false },
    //                 // { id: 1, path: 'tu/kouhong2', pos: cc.v2(0, 0), boards: [], isGray: false },
    //                 // { id: 1, path: 'tu/liwu1', pos: cc.v2(0, 0), boards: [], isGray: false },
    //                 // { id: 1, path: 'tu/liwu2', pos: cc.v2(0, 0), boards: [], isGray: false },
    //                 // { id: 1, path: 'tu/matong1', pos: cc.v2(0, 0), boards: [], isGray: false },
    //                 // { id: 1, path: 'tu/matong2', pos: cc.v2(0, 0), boards: [], isGray: false },
    //                 // { id: 1, path: 'tu/mianju1', pos: cc.v2(0, 0), boards: [], isGray: false },
    //                 // { id: 1, path: 'tu/mianju2', pos: cc.v2(0, 0), boards: [], isGray: false },
    //                 // { id: 1, path: 'tu/niu1', pos: cc.v2(0, 0), boards: [], isGray: false },
    //                 // { id: 1, path: 'tu/niu2', pos: cc.v2(0, 0), boards: [], isGray: false },
    //                 // { id: 1, path: 'tu/mianbao1', pos: cc.v2(0, 0), boards: [], isGray: false },
    //                 // { id: 1, path: 'tu/mianbao2', pos: cc.v2(0, 0), boards: [], isGray: false },
    //                 // { id: 1, path: 'tu/shuilongtou1', pos: cc.v2(0, 0), boards: [], isGray: false },
    //                 // { id: 1, path: 'tu/shuilongtou2', pos: cc.v2(0, 0), boards: [], isGray: false },
    //                 // { id: 1, path: 'tu/tianshi1', pos: cc.v2(0, 0), boards: [], isGray: false },
    //                 // { id: 1, path: 'tu/tianshi2', pos: cc.v2(0, 0), boards: [], isGray: false },
    //                 // { id: 1, path: 'tu/huoche1', pos: cc.v2(0, 0), boards: [], isGray: false },
    //                 // { id: 1, path: 'tu/huoche2', pos: cc.v2(0, 0), boards: [], isGray: false },
    //                 // { id: 1, path: 'tu/xiaogou1', pos: cc.v2(0, 0), boards: [], isGray: false },
    //                 // { id: 1, path: 'tu/xiaogou2', pos: cc.v2(0, 0), boards: [], isGray: false },
    //                 // { id: 1, path: 'tu/youhu1', pos: cc.v2(0, 0), boards: [], isGray: false },
    //                 // { id: 1, path: 'tu/youhu2', pos: cc.v2(0, 0), boards: [], isGray: false },
    //                 { id: 1, path: 'tu/youxishoubing1', boards: [], isGray: false },
    //                 { id: 1, path: 'tu/youxishoubing2', boards: [], isGray: false },
    //                 { id: 1, path: 'layer/layer16', boards: [], isGray: true },
    //             ]
    //     },
    //     {
    //         id: 2,
    //         colorKind: 6,
    //         carNums: [0, 0, 0],
    //         layers:
    //             [
    //                 { id: 1, path: 'tu/bingtong1', pos: cc.v2(0, 0), boards: [], isGray: false },
    //                 { id: 1, path: 'tu/bingtong2', pos: cc.v2(0, 0), boards: [], isGray: false },
    //                 { id: 1, path: 'layer/layer2', pos: cc.v2(0, 0), boards: [], isGray: true },          
    //             ]
    //     },
    // ];

    private getSpecialName(lv: number, id: number) {
        const names = [
            'biji', 'chuizi', 'dianhua', 'gaodian', 'guo', 'juchang', 'kouhong', 'liwu', 'matong', 'mianju',
            'niu', "mianbao", "shuilongtou", "tianshi", "huoche", "xiaogou", "youhu", "youxishoubing", "bingtong"
        ];
        if (lv > names.length) {
            lv = lv % names.length;
        }
        const name = `tu/${names[lv - 1]}${id}`;
        return name;
    }

    public getLayerName() {
        const index = Math.floor(Math.random() * 21); // TODO 几何图层数
        return `layer/layer${index}`;
    }

    public getLevelConfigById(id: number) {
        const colorKind = id === 1 ? 2 : 8;      // TODO 当前关颜色种数
        const config = {
            id: id,
            colorKind: colorKind,
            carNums: [0, 0, 0],
            layers:
                [
                    { id: 1, path: this.getSpecialName(id, 1), boards: [], isGray: false },
                    { id: 1, path: this.getSpecialName(id, 2), boards: [], isGray: false },
                    { id: 1, path: `layer/layer${id}`, boards: [], isGray: true },
                ]
        }
        return JSON.parse(JSON.stringify(config));
    }

    public getLayerConfigById(id: number, layerId: number) {
        const config = this.getLevelConfigById(id);
        const layer = config.layers.find((o) => o.id === layerId);
        return layer;
    }

    public getBoardConfigById(id: number, layerId: number, boardId: number) {
        const layer = this.getLayerConfigById(id, layerId);
        const board = layer.boards.find((o) => o.id === boardId);
        return board;
    }

    public getScrewConfigById(id: number, layerId: number, boardId: number, screwId: number) {
        const board = this.getBoardConfigById(id, layerId, boardId);
        const screw = board.screwInfos.find((o) => o.id === screwId);
        return screw;
    }
}